package com.task.myapplication11;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import java.util.ArrayList;

public class MyReceiver extends BroadcastReceiver {
    GetMess getMess;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        String s=intent.getAction();
        if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(s))
        {
            int p=intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-100);
            if(p==BluetoothAdapter.STATE_TURNING_OFF)
            {
                Toast.makeText(context, "蓝牙关闭", Toast.LENGTH_SHORT).show();
            }
            else if(p==BluetoothAdapter.STATE_TURNING_ON){
                Toast.makeText(context, "蓝牙开启", Toast.LENGTH_SHORT).show();
            }
        }
        else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(s))
        {
            Toast.makeText(context, "搜索开始", Toast.LENGTH_SHORT).show();
        }
        //找到远程设备
        else if(BluetoothDevice.ACTION_FOUND.equals(s)){
            //Toast.makeText(context,"找到",Toast.LENGTH_SHORT).show();
            BluetoothDevice device=intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE);
            getMess.mess(device.getName()+" "+device.getAddress());
        }
        else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(s))
        {
            Toast.makeText(context, "搜索完毕", Toast.LENGTH_SHORT).show();
            getMess.finish();
        }
        //throw new UnsupportedOperationException("Not yet implemented");
    }
    public interface GetMess{
        void mess(String s);
        void finish();
    }
    public void setGetMessListener(GetMess brInteraction) {
        this.getMess = brInteraction;
    }
}